﻿using System.Collections.Specialized;

namespace ncp.Business.Common
{
    public class WebMasterCookie
    {
        private const string WebMasterCookieName = "LOGINUSERINFO";

        /// <summary>
        /// 保存cookie（默认1天)
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddCookie(UserInfo info)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}
                                 };
            return CookieHelper.WriteCookie(WebMasterCookieName, collection);
        }
        public static bool AddCookie(UserInfo info,string cookieName)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}

                                 };
            return CookieHelper.WriteCookie(cookieName, collection);
        }

        public static bool AddCookie(UserInfo info,int days)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}
                                 };
            return CookieHelper.WriteCookie(WebMasterCookieName, collection,days);
        }

        public static bool AddCookie(UserInfo info, int days,string cookieName)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}
                                 };
            return CookieHelper.WriteCookie(cookieName, collection, days);
        }

        /// <summary>
        /// 更新cookie
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool UpdateCookie(UserInfo info)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}
                                 };
            return CookieHelper.UpdateCookie(WebMasterCookieName, collection);
        }
        public static bool UpdateCookie(UserInfo info,string cookieName)
        {
            var collection = new NameValueCollection
                                 {
                                     {"guid",info.UserGuid==null?"": EncryptHelper.EncryptString(info.UserGuid)},
                                     {"id",info.Id==null?"": EncryptHelper.EncryptString(info.Id)},
                                     {"userid", info.UserId==null?"":EncryptHelper.EncryptString(info.UserId)},
                                     {"password", info.Password==null?"":EncryptHelper.EncryptString(info.Password)},
                                     {"uname",info.UName==null?"":EncryptHelper.EncryptString(info.UName)},
                                     {"relname",info.RelName==null?"": EncryptHelper.EncryptString(info.RelName)},
                                     {"sex", info.Sex==null?"":EncryptHelper.EncryptString(info.Sex)},
                                     {"email",info.Email==null?"": EncryptHelper.EncryptString(info.Email)},
                                     {"status", info.Status==null?"":EncryptHelper.EncryptString(info.Status)},
                                     {"computerip", info.Computerip==null?"":EncryptHelper.EncryptString(info.Computerip)},
                                     {"computername", info.Computername==null?"":EncryptHelper.EncryptString(info.Computername)}
                                 };
            return CookieHelper.UpdateCookie(cookieName, collection);
        }

        /// <summary>
        /// 获取cookie
        /// </summary>
        /// <returns></returns>
        public static UserInfo GetCookie()
        {
            var nvc = CookieHelper.GetCookie(WebMasterCookieName);
            if (nvc==null)
            {
                return null;
            }
            var info = new UserInfo
                           {
                               UserGuid = nvc.Get("guid") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("guid")),
                               Id = nvc.Get("id") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("id")),
                               UName = nvc.Get("uname") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("uname")),
                               UserId = nvc.Get("userid") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("userid")),
                               Password = nvc.Get("password") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("password")),
                               RelName = nvc.Get("relname") == "" ? "" : nvc.Get("relname") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("relname")),
                               Sex = nvc.Get("sex") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("sex")),
                               Status = nvc.Get("status") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("status")),
                               Email = nvc.Get("email") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("email")),
                               Computerip = nvc.Get("computerip") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("computerip")),
                               Computername = nvc.Get("computername") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("computername"))
                           };
            return info;
        }
        public static UserInfo GetCookie(string cookieName)
        {
            var nvc = CookieHelper.GetCookie(cookieName);
            if (nvc==null)
            {
                return null;
            }
            var info = new UserInfo
                           {
                               UserGuid = nvc.Get("guid") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("guid")),
                               Id = nvc.Get("id") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("id")),
                               UName = nvc.Get("uname") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("uname")),
                               UserId = nvc.Get("userid") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("userid")),
                               Password = nvc.Get("password") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("password")),
                               RelName = nvc.Get("relname") == "" ? "" : nvc.Get("relname") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("relname")),
                               Sex = nvc.Get("sex") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("sex")),
                               Status = nvc.Get("status") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("status")),
                               Email = nvc.Get("email") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("email")),
                               Computerip = nvc.Get("computerip")==""?"":EncryptHelper.DecryptString(nvc.Get("computerip")),
                               Computername = nvc.Get("computername") == "" ? "" : EncryptHelper.DecryptString(nvc.Get("computername"))
                           };
            return info;
        }
        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <returns></returns>
        public static bool DeleteCookie()
        {
           return CookieHelper.DeleteCookie(WebMasterCookieName);
        }
        public static bool DeleteCookie(string cookieName)
        {
            return CookieHelper.DeleteCookie(cookieName);
        }
    }
}
